package com.weixin.base.dao.impl;

import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;

import cn.org.rapid_framework.page.Page;
import cn.org.rapid_framework.page.PageRequest;

import com.weixin.base.dao.AdminReplyDao;
import com.weixin.base.util.Tools;
import com.weixin.common.BaseSpringJdbcDao;
import com.weixin.entity.TbWxMessage;
import com.weixin.entity.TbWxUser;

public class AdminReplyDaoImpl extends BaseSpringJdbcDao<TbWxUser, Integer> implements AdminReplyDao {

	public Class getEntityClass() {
		return TbWxUser.class;
	}
	
	/**
	 * 查询用户所有自定义回复key值
	 */
	public List<Map<String,Object>> selectAdminUsers(){
		String sql = "SELECT nUserId,sName  FROM tbbguser where ntype=11 and nIsValid=1 ";
		return getSimpleJdbcTemplate().queryForList(sql);
	}
	
	/**
	 * 查询用户所有自定义回复key值
	 */
	public List<Map<String,Object>> selectUserKeys(int userid){
		String sql = "SELECT distinct(nKey),nKey FROM tbwxmessage where nIsValid=1 and nUserId=" + userid;
		return getSimpleJdbcTemplate().queryForList(sql);
	}

	/**
	 * 查询用户所有跳转链接
	 */
	public List<Map<String, Object>> selectUserUrl(int userid) {
		String sql = "SELECT sName,nWxUserUrlId FROM tbwxuserurl where nIsValid=1 and nUserId=" + userid;
		return getSimpleJdbcTemplate().queryForList(sql);
	}

	/**
	 * 添加关注消息记录 result 返回值：1.成功 2.失败 3.图片格式不正确（仅支持JPG、PNG）
	 */
	public int saveReplyNotice(TbWxMessage tbWxMessage) {

		// 获取链接地址
		tbWxMessage.setSWxUserUrl(selectWxUserUrl(tbWxMessage.getNWxUserUrlId()));

		String sql = "INSERT INTO tbwxmessage (nUserId,sTitle,sDescription,sPicUrl,nWxUserUrlId,sWxUserUrl,ntype,nReplyType,nIsValid,dCreateTime,nOrder,nKey) "
				+ " VALUES (:nUserId,:sTitle,:sDescription,:sPicUrl,:nWxUserUrlId,:sWxUserUrl,:ntype,:nReplyType,:nIsValid,:dCreateTime,:nOrder,:nKey)";
		int result = getNamedParameterJdbcTemplate().update(sql, new BeanPropertySqlParameterSource(tbWxMessage));

		return result > 0 ? 1 : 2;

	}

	public String selectWxUserUrl(int wxUserUrlId) {
		if (wxUserUrlId == 0) {
			return "";
		}
		String sql = "SELECT sUrl FROM tbwxuserurl where nWxUserUrlId=" + wxUserUrlId;
		return getSimpleJdbcTemplate().queryForObject(sql, String.class);
	}

	/**
	 * 查询关注消息 return pageRequest:分页类 userid:用户ID nReplyType：消息回复类型（1.关注回复 2自动回复 3自定义菜单回复 4关键字回复）
	 * ntype：消息类型（1.文本 2图片 3语音 4视频 5 音乐 6图文）
	 */
	public Page searchReplyNotice(PageRequest<Map> pageRequest, int userid, int nReplyType, int ntype) {
		Map map = pageRequest.getFilters();
		String sql = "";
		String order = "  order by nOrder ";
		int nkey=0;
		
		if(map.get("nkey") != null){
			nkey = Tools.convertRi(map.get("nkey"));
		}
			
		sql += " AND nUserId=" + userid + " AND ntype=" + ntype + " AND nReplyType=" + nReplyType + " ";

		if (nkey != 0) {
			sql += " AND nKey=" + nkey + " ";
		}
		
		String sql_count = "SELECT count(1) FROM tbwxmessage with (nolock) WHERE 1=1 " + sql;
		String sql_page = "SELECT nWxMessageId,nUserId,sTitle,sDescription,sPicUrl,nwxUserUrlId,ntype,nReplyType,nIsValid,dCreateTime,nOrder,nKey FROM tbwxmessage with (nolock) WHERE 1=1 "
				+ sql + order;
		return pageQuery(sql_page, sql_count, pageRequest, new BeanPropertyRowMapper(TbWxMessage.class));
	}

	/**
	 * 修改关注消息记录 result 返回值：1.成功 2.失败 3.图片格式不正确（仅支持JPG、PNG）
	 * tbWxMessage.getSPicUrl()值为1表示不需要更改图片
	 */
	public int updateReplyNotice(TbWxMessage tbWxMessage) {

		// 获取链接地址
		tbWxMessage.setSWxUserUrl(selectWxUserUrl(tbWxMessage.getNWxUserUrlId()));

		String sql = " UPDATE tbwxmessage set ";
		if (tbWxMessage.getSPicUrl().equals("1")) {
			sql += " sTitle = :sTitle,sDescription = :sDescription,nWxUserUrlId = :nWxUserUrlId,sWxUserUrl=:sWxUserUrl,nIsValid = :nIsValid,nOrder = :nOrder,nKey=:nKey ";
		} else {
			sql += " sTitle = :sTitle,sDescription = :sDescription,sPicUrl = :sPicUrl,nWxUserUrlId = :nWxUserUrlId,sWxUserUrl=:sWxUserUrl,nIsValid = :nIsValid,nOrder = :nOrder,nKey=:nKey ";
		}
		sql += " WHERE nWxMessageId=:nWxMessageId ";
		int result = getNamedParameterJdbcTemplate().update(sql, new BeanPropertySqlParameterSource(tbWxMessage));

		return result > 0 ? 1 : 2;
	}

	/**
	 * 删除关注消息记录 result 返回值：1.成功 2.失败
	 */
	public int delReplyNotice(int nWxMessageId) {
		String sql = " DELETE FROM tbwxmessage where nWxMessageId=" + nWxMessageId;
		int result = getSimpleJdbcTemplate().update(sql);
		return result > 0 ? 1 : 2;
	}
}
